﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Imports TemplateParser

Partial Class FrontEnd_Control_Users_Register
    Inherits System.Web.UI.UserControl

    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadType()
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            Page.Title = "Đăng ký | " & DomainName
            LoadDate()
            hplDangNhap.NavigateUrl = MainDomain & "dang-nhap.html"
        End If
    End Sub
    Protected Sub LoadType()
        Using data As New VSEntities
            'Khu vuc
            ddlKhuVuc.AppendDataBoundItems = True
            ddlKhuVuc.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim ba = (From ca In data.DonViHanhChinhs Select ca.Id, ca.Title).ToList
            ddlKhuVuc.DataTextField = "Title"
            ddlKhuVuc.DataValueField = "ID"
            ddlKhuVuc.DataSource = ba
            ddlKhuVuc.DataBind()
        End Using
    End Sub

    Private Sub LoadDate()
        ddlDay.AppendDataBoundItems = True
        ddlDay.Items.Add(New ListItem("Ngày", "0"))
        ddlDay.DataSource = Enumerable.Range(1, 31)
        ddlDay.DataBind()
        ddlDay.SelectedValue = 0

        ddlMonth.AppendDataBoundItems = True
        ddlMonth.Items.Add(New ListItem("Tháng", "0"))
        ddlMonth.DataSource = Enumerable.Range(1, 12)
        ddlMonth.DataBind()
        ddlMonth.SelectedValue = 0

        ddlYear.AppendDataBoundItems = True
        ddlYear.Items.Add(New ListItem("Năm", "0"))
        'chi cho dang ky tu 1945 va < hien tai -10
        ddlYear.DataSource = Enumerable.Range(1945, Now.Year - (1945 + 10))
        ddlYear.DataBind()
        ddlYear.SelectedValue = 0
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegister.Click
        Using data As New VSEntities
            Dim k = (From q In data.Users Where q.UserName = txtUsername.Text Select q).FirstOrDefault
            If Not k Is Nothing Then
                Excute_Javascript("Alertbox('Người dùng đã tồn tại hãy chọn tên khác.');", Me.Page)
            Else
                Dim p As New VSModel.User
                Try
                    p.UserName = txtUsername.Text.Trim()
                    p.Password = Cls_Common.Encrypt(txtPassword.Text.Trim())

                    p.FullName = txtFist.Text.Trim() & " " & txtLastname.Text.Trim
                    p.IsUser = 5
                    p.Created = Now
                    p.IsActivated = True
                    p.IsShowHome = True
                    p.IsAdmin = False
                    p.Gender = ddlGender.SelectedValue
                    p.Email = txtEmail.Text.Trim
                    p.Phone = txtDienThoai.Text.Trim()
                    p.MobileOrther = txtDienThoaiKhac.Text.Trim
                    p.DiaChi = txtDiaChi.Text.Trim()
                    p.TinhTP = ddlKhuVuc.SelectedValue
                    p.DateOfBirth = StringToDate(ddlDay.SelectedValue.ToString & "/" & ddlMonth.SelectedValue.ToString & "/" & ddlYear.SelectedValue.ToString, "dd/mm/yyyy")
                    p.UserTypeId = 4

                    'enable
                    p.IsActivated = True

                    data.Users.AddObject(p)
                    data.SaveChanges()
                    p.Code = FormatCode(p.UserId.ToString)
                    data.SaveChanges()
                    'send email thong bao
                    Dim templatevars As New Hashtable()
                    templatevars.Add("firstname", txtFist.Text)
                    templatevars.Add("lastname", txtLastname.Text)
                    templatevars.Add("login", txtUsername.Text)
                    templatevars.Add("password", txtPassword.Text)
                    Dim parser As New Parser(Server.MapPath("~/frontend/emailtemplate/registration.htm"), templatevars)

                    Sendmail_SMTP("no-reply@hoanvustc.com.vn", "no-reply", "mail.hoanvustc.com.vn", txtEmail.Text, "", "hoanvu dang ky tai khoan thanh cong", parser.Parse, "123IGKs1233!@#", 25, False)

                    Session("UserName") = p.UserName
                    Session("UserID") = p.UserId
                    Session("UserVip") = p.Vip
                    'Insert_App_Log("Insert Member:" & txtUsername.Text.Trim & "", Function_Name.User, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("alert('Đăng ký thành viên thành công.');init();", Me.Page, True)

                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("alert('Đăng ký thành viên thất bại hãy kiểm tra lại đường truyền hoặc liên hệ với quản lý.');", Me.Page, True)
                End Try
            End If

        End Using
    End Sub
   
#End Region

    
End Class
